package com.ghost.librariandb;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.util.Date;

import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.MaskFormatter;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JFormattedTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

public class TakeBook extends JPanel
{
	private JFormattedTextField txtBookID;
	private JLabel lblBookID;
	private JLabel lblBookName;
	private JLabel lblAuthor;
	private JFormattedTextField txtBorrowerID;
	private JLabel lblBorrowerName;
	private boolean isTakenBook = true;
	private JButton btnUpdate;

	/**
	 * Create the panel.
	 */
	public TakeBook()
	{
		setLayout(null);
		
		JLabel lblBookId = new JLabel("Book ID:");
		lblBookId.setBounds(10, 11, 46, 14);
		add(lblBookId);
		
		txtBookID = new JFormattedTextField(new Integer(0));
		txtBookID.setBounds(70, 8, 127, 20);
		add(txtBookID);
		
		JButton btnGetBook = new JButton("Get Book");
		btnGetBook.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				int bookID = (int) txtBookID.getValue();
				if (bookID > 0)
				{
					Connection connection = DataProvider.getConnection();
					Statement statement = null;
					try
					{
						statement = connection.createStatement();
						statement.setQueryTimeout(30);
						ResultSet rs = statement.executeQuery("select * from Book where ID = " + Integer.toString(bookID));
//						ResultSet rs = statement.executeQuery("select * from Book");
						int count = 0;
						while (rs.next())
						{
							count++;
							lblBookID.setText(rs.getString("ID"));
							lblBookName.setText(rs.getString("Name"));
							lblAuthor.setText(rs.getString("Author"));
							txtBorrowerID.setValue(rs.getInt("BorrowerID"));
							if (rs.getInt("BorrowerID") > 0)
							{
								isTakenBook = true;
								txtBorrowerID.setEnabled(false);
							}
							else
							{
								txtBorrowerID.setEnabled(true);
								isTakenBook = false;
							}
							break;
						}
						System.out.println("Record found: " + count);
					}
					catch (SQLException ex)
					{
						ex.printStackTrace();
					}
					finally
					{
						if (statement != null)
						{
							try
							{
								statement.close();
							}
							catch (SQLException ex)
							{
								ex.printStackTrace();
							}
						}
					}
				}
			}
		});
		btnGetBook.setBounds(200, 7, 89, 23);
		add(btnGetBook);
		
		JLabel lblLabel1 = new JLabel("Book ID:");
		lblLabel1.setBounds(10, 50, 90, 14);
		add(lblLabel1);
		
		lblBookID = new JLabel("");
		lblBookID.setBounds(122, 50, 256, 14);
		add(lblBookID);
		
		JLabel lblLabel2 = new JLabel("Book Name:");
		lblLabel2.setBounds(10, 66, 90, 14);
		add(lblLabel2);
		
		lblBookName = new JLabel("");
		lblBookName.setBounds(122, 66, 256, 14);
		add(lblBookName);
		
		JLabel lblALabel3 = new JLabel("Author:");
		lblALabel3.setBounds(10, 81, 90, 14);
		add(lblALabel3);
		
		lblAuthor = new JLabel("");
		lblAuthor.setBounds(122, 81, 256, 14);
		add(lblAuthor);
		
		JLabel lblBorrowerId = new JLabel("Borrower ID:");
		lblBorrowerId.setBounds(10, 106, 90, 14);
		add(lblBorrowerId);
		
		txtBorrowerID = new JFormattedTextField(new Integer(0));
		txtBorrowerID.setEnabled(false);
		txtBorrowerID.setBounds(122, 104, 107, 20);
		txtBorrowerID.addPropertyChangeListener(new PropertyChangeListener()
		{
			@Override
			public void propertyChange(PropertyChangeEvent event)
			{
				System.out.println("Value Changed.");
				int borrowerID = (int) txtBorrowerID.getValue();
				Connection connection = DataProvider.getConnection();
				Statement statement = null;
				try
				{
					statement = connection.createStatement();
					statement.setQueryTimeout(30);
					ResultSet rs = statement.executeQuery("select * from Member where ID = " + Integer.toString(borrowerID));
//					ResultSet rs = statement.executeQuery("select * from Book");
					int count = 0;
					while (rs.next())
					{
						count++;
						lblBorrowerName.setText(rs.getString("Name"));
						if (isTakenBook == false)
						{
							btnUpdate.setEnabled(true);
						}
						else
						{
							btnUpdate.setEnabled(false);
						}
						break;
					}
					System.out.println("Record found: " + count);
					if (count <= 0)
					{
						btnUpdate.setEnabled(false);
						lblBorrowerName.setText("");
					}
				}
				catch (SQLException ex)
				{
					ex.printStackTrace();
				}
				finally
				{
					if (statement != null)
					{
						try
						{
							statement.close();
						}
						catch (SQLException ex)
						{
							ex.printStackTrace();
						}
					}
				}
			}
		});
		add(txtBorrowerID);
		
		btnUpdate = new JButton("Update");
		btnUpdate.setEnabled(false);
		btnUpdate.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				Date now = new Date();
				Connection connection = DataProvider.getConnection();
				Statement statement = null;
				try
				{
					statement = connection.createStatement();
					statement.setQueryTimeout(30);
					String sql = "update Book set BorrowerID = " + txtBorrowerID.getValue() + ", BorrowedDate = " + Long.toString(now.getTime()) + " where ID = " + lblBookID.getText();
					System.out.println(sql);
					statement.executeUpdate(sql);
					btnUpdate.setEnabled(false);
				}
				catch (SQLException ex)
				{
					ex.printStackTrace();
				}
				finally
				{
					if (statement != null)
					{
						try
						{
							statement.close();
						}
						catch (SQLException ex)
						{
							ex.printStackTrace();
						}
					}
				}
			}
		});
		btnUpdate.setBounds(122, 169, 89, 23);
		add(btnUpdate);
		
		JLabel lblLabel4 = new JLabel("Borrower Name:");
		lblLabel4.setBounds(10, 131, 102, 14);
		add(lblLabel4);
		
		lblBorrowerName = new JLabel("");
		lblBorrowerName.setBounds(122, 132, 200, 14);
		add(lblBorrowerName);

	}
}
